package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.LineItem;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.AdminService;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/adminOrder")
public class AdminOrderController {
    @Autowired
    AdminService adminService;

    @Autowired
    CatalogService catalogService;

    //管理员处理订单
    @GetMapping("/handleOrder")
    public String handleOrder(Model model, String orderId){
        adminService.upDateOrderStatus(orderId);
        List<Order> orderList = adminService.getUnhandledOrders();

        model.addAttribute("orderList",orderList);

        return "/EditOrder/EditOrder";
    }

    //管理员查看用户订单
    @GetMapping("/adminViewOrder")
    public String adminViewOrder(Model model, String orderId){
        int orderInt=Integer.decode(orderId);

        Order order = adminService.getOrderByOrderId(orderInt);
        List<LineItem> lineItemList = adminService.getLineItemsByOrderId(orderInt);
        for (LineItem li:lineItemList
             ) {
            li.setItem(catalogService.getItem(li.getItemId()));
        }

        order.setLineItems(lineItemList);
        model.addAttribute("order",order);

        return "EditOrder/ViewUserOrder";
    }
}
